package net.hn.hnms.biz.safety.trinity.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.api.safety.trinity.model.ViolationRecordDto;
import net.hn.hnms.biz.api.safety.trinity.model.ViolationRecordVo;
import net.hn.hnms.biz.common.jpa.utils.ResponsePage;
import net.hn.hnms.biz.safety.trinity.entity.OriginalViolationRecordVo;
import net.hn.hnms.biz.safety.trinity.service.WorkSafetyViolationService;
import net.hn.hnms.biz.safety.trinity.until.DictData;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.excel.utils.ExcelUtil;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 智呈物联 pan'sx
 * 2022/7/26 15:17
 * 1.0 version
 */
@RestController
@RequestMapping("manage/violationRecord")
@Api(value = "安全三违台账控制器", tags = {"安全三违台账"})
public class ViolationRecordController {

    WorkSafetyViolationService workSafetyViolationService;



    @Autowired
    public void setWorkSafetyViolationService(WorkSafetyViolationService workSafetyViolationService) {
        this.workSafetyViolationService = workSafetyViolationService;
    }

    @GetMapping("page")
    @ApiOperation("安全三违 分页查询")
    @AutoSetMineCode
    public R<ResponsePage<ViolationRecordVo>> page(PageQuery pageQuery, ViolationRecordDto violationRecordDto,String mineCode) {
        return R.ok(workSafetyViolationService.page(pageQuery,violationRecordDto,mineCode));
    }

    @GetMapping("/original/page")
    @ApiOperation("安全三违 原始 分页查询")
    @AutoSetMineCode
    public R<ResponsePage<OriginalViolationRecordVo>> originalPage(PageQuery pageQuery, ViolationRecordDto violationRecordDto, String mineCode) {
        return R.ok(workSafetyViolationService.originalPage(pageQuery,violationRecordDto,mineCode));
    }

    @GetMapping(value = "export", produces = "application/octet-stream")
    @ApiOperation(value = "导出数据")
    @AutoSetMineCode
    public void export(HttpServletResponse response, ViolationRecordDto violationRecordDto,String mineCode) throws IOException {
        String fileName = "三违台账";
        PageQuery pageQuery=new PageQuery();
        pageQuery.setPageNum(0);
        pageQuery.setPageSize(10000);
        ResponsePage<ViolationRecordVo> recordVoResponsePage = workSafetyViolationService.page(pageQuery,violationRecordDto,mineCode);

        List<ViolationRecordVo> list = recordVoResponsePage.getPageData();
        DictData dictData = new DictData();
//        Map<String,String> violationType = dictData.dictToMap("violation_type");
//        Map<String,String> violationLevel = dictData.dictToMap("violation_level");
//
//        //为字典项赋值
//        for (ViolationRecordVo violationRecordVo:list){
//            String vioType = violationType.get(violationRecordVo.getType());
//            if (vioType!=null){
//                violationRecordVo.setType(vioType);
//            }
//
//            String vioLevel = violationLevel.get(violationRecordVo.getLevel());
//            if (vioLevel!=null){
//                violationRecordVo.setLevel(vioLevel);
//            }
//
//        }

        ExcelUtil.exportExcel(list,fileName, ViolationRecordVo.class,response);
    }
}
